/**
 * 
 */
package kulstats.server;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kulstats.Registry;
import kulstats.User;
import kulstats.UserManager;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * @author steven
 *
 */
public class LoginServlet extends HttpServlet {

    @Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws IOException {
    	
    	UserService userService = UserServiceFactory.getUserService();
    	com.google.appengine.api.users.User aeUser = userService.getCurrentUser();
    	
    	if(aeUser == null) {
    		//login failed, redirect back to login page
    		resp.sendRedirect("/login");
    	}
    	else {
    		String UserID  = aeUser.getUserId();
    		UserManager userManager = Registry.userManager();
    		User user = userManager.getUser(UserID);
    		if(user != null) {
    			//user recognized, login successfully logged in
    			//redirect to homepage
    			resp.sendRedirect("/home");
    		}
    		else {
    			//user not recognized in database -> new user -> need additional information
    			//redirect to register page for additional information
    			resp.sendRedirect("/register");
    		}
    	}
    }
    
    @Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
    		throws IOException {
    }
}
